$("#signalMachineModal4Audit input").attr("readonly", true);
$("#signalMachineModal4Audit select").attr("disabled", true);
$(function () {
    gMainDT = $("#mainDataTable").DataTable({
        ajax: {
            url: "../rds_signalMachineManage/getSignalMachine4DT",
            type: "POST",
            data: function (d) {
                d.search = $('#mainDataTable').DataTable().search(this.value);
                d.actorIds = gUserLoginResult.userActors;
                d.orgId = gUserLoginResult.orgId;
                d.audit = true;
                d.startRoad = "";
                d.endRoad = "";
                d.roadName = "";
                d.saveState = "";
            }
        },
        columns: [{
            data: "signalMachineId",
            render: function (data, type, row, meta) {
                var checkId = "checkbox" + (parseInt(meta.row) + 1);
                return "<input type='checkbox' value='" + data + "' id='" + checkId + "'>";
            }
        }, {
            data: null,
            targets: 0
        }, {
            data: "signalMachinePhoto",
            render: function (data, type, row) {
                return "<img src='" + data + "' style='width: 50px; height: 40px; cursor: pointer' " +
                    "onclick='$.showPicModal(\"" + data + "\")'>";
            }
        }, {
            data: null,
            render: function (data, type, row) {
                return row.signalMachineProvince + " " + row.signalMachineCity + " " + row.signalMachineArea;
            }
        }, {
            data: null,
            render: function (data, type, row) {
                return row.signalMachineStartRoad + "——" + row.signalMachineEndRoad;
            }
        }, {
            data: "signalMachineOrgName"
        }, {
            data: "signalMachineCreatorName"
        }, {
            data: "signalMachineInstallTime"
        }, {
            data: "signalMachineId",
            render: function (data, type, row) {
                var buttonOperation = "";
                var buttonAudit = "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.auditSignalMachine(" + data + ")' title='审核'>" +
                    "<i class='glyphicon glyphicon-check'></i></button> ";
                gCheck == 0 ? buttonOperation : buttonOperation += buttonAudit;

                buttonOperation += "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.showSignalMachine(" + JSON.stringify(row) + ")' title='查看'>" +
                    "<i class='glyphicon glyphicon-eye-open'></i></button> ";

                var buttonDelete = "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.deleteSignalMachine(" + data + ")' title='删除'>" +
                    "<i class='fa fa-minus-square'></i></button>";
                gDelete == 0 ? buttonOperation : buttonOperation += buttonDelete;

                return buttonOperation;
            }
        }],
        dom: "<'row'<'col-md-4'l><'#mainToolbar.col-md-8'>r>t<'row'<'col-md-5'i><'col-md-7'p>>",
        initComplete: function () {
            // 下拉工具栏按钮的权限
            $("#mainToolbar").empty();
            var strToolBar = "<div class='pull-right'><div class='btn-group'>" +
                "<button type='button' class='btn btn-primary btn-sm btn-flat'><i class='fa fa-cog'></i> 操作</button>" +
                "<button type='button' class='btn btn-primary btn-sm btn-flat dropdown-toggle' " +
                "data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>" +
                "<span class='caret'></span><span class='sr-only'>切换下拉</span></button>" +
                "<ul class='dropdown-menu'>" +
                "<li>" +
                "<button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='gMainDT.ajax.reload(null, false);'><i class='fa fa-refresh'></i> 刷新</button>" +
                "</li>";

            var buttonAudit = "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='$.auditSignalMachine(0)'><i class='fa fa-plus-square'></i> 审核</button></li>";
            gCheck == 0 ? strToolBar : strToolBar += buttonAudit;

            var buttonDelete = "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='$.deleteSignalMachine(0)'><i class='fa fa-minus-square'></i> 删除</button></li>";
            gDelete == 0 ? strToolBar : strToolBar += buttonDelete;

            $("#mainToolbar").append(strToolBar);
        },
        processing: true,
        serverSide: true,
        ordering: false,
        select: false,
        autoFill: true,
        displayLength: 20,
        lengthMenu: [[5, 10, 15, 20, 50, 100, 150, -1], [5, 10, 15, 20, 50, 100, 150, "All"]],
        language: {
            url: "zh_cn.json"
        },
        fnDrawCallback: function () {// 序号自增回调函数
            var api = this.api();
            var startIndex = api.context[0]._iDisplayStart;
            api.column(1).nodes().each(function (cell, i) {
                cell.innerHTML = startIndex + i + 1;
            });
        }
    });
});

$.fn.searchDT = function () {
    gMainDT.search().draw();
};

$.checkChange = function () {
    var isChecked = $("#batchCheck").prop('checked');
    $("input:checkbox[id^='checkbox']").prop("checked", isChecked);
};

$.auditSignalMachine = function (signalMachineId) {
    var count = 1;
    if (signalMachineId == 0) {
        count = $("input:checkbox[id^='checkbox']:checked").length;
        if (count == 0) {
            $.alert("请选择要审核的数据", 2);
            return;
        } else {
            signalMachineId = "";
            $("input:checkbox[id^='checkbox']:checked").each(function () {
                signalMachineId += $(this).val() + ",";
            });
            signalMachineId = signalMachineId.substring(0, signalMachineId.length - 1);
        }
    }
    bootbox.confirm({
        buttons: {
            confirm: {
                label: '<i class="fa fa-check"></i> 确定',
                className: 'btn btn-primary btn-flat'
            },
            cancel: {
                label: '<i class="fa fa-close"></i> 取消',
                className: 'btn btn-warning btn-flat pull-left'
            }
        },
        message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">确定审核通过该' + count + '条数据吗?</b>',
        callback: function (result) {
            if (result) {
                $.ajax({
                    url: '../rds_signalMachineManage/auditSignalMachine',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        "signalMachineIds": signalMachineId
                    },
                    success: function (msg) {
                        if (msg != null && typeof (msg) != "undefined") {
                            if (msg.isSuccess == true) {
                                $.alert("数据审核通过成功", 1);
                                gMainDT.ajax.reload(null, false);
                            } else {
                                $.alert("数据审核通过失败", 2);
                            }
                        } else {
                            $.alert('系统错误，请联系管理员', 2);
                        }
                    },
                    error: function () {
                        $.alert('系统错误，请联系管理员', 2);
                    }
                });
            }
        }
    });
};

$.deleteSignalMachine = function (signalMachineId) {
    var count = 1;
    if (signalMachineId == 0) {
        count = $("input:checkbox[id^='checkbox']:checked").length;
        if (count == 0) {
            $.alert("请选择要删除的数据", 2);
            return;
        } else {
            signalMachineId = "";
            $("input:checkbox[id^='checkbox']:checked").each(function () {
                signalMachineId += $(this).val() + ",";
            });
            signalMachineId = signalMachineId.substring(0, signalMachineId.length - 1);
        }
    }
    bootbox.confirm({
        buttons: {
            confirm: {
                label: '<i class="fa fa-check"></i> 确定',
                className: 'btn btn-primary btn-flat'
            },
            cancel: {
                label: '<i class="fa fa-close"></i> 取消',
                className: 'btn btn-warning btn-flat pull-left'
            }
        },
        message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">确定删除该' + count + '条数据吗?</b>',
        callback: function (result) {
            if (result) {
                $.ajax({
                    url: '../rds_signalMachineManage/deleteSignalMachine',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        "signalMachineIds": signalMachineId
                    },
                    success: function (msg) {
                        if (msg != null && typeof (msg) != "undefined") {
                            if (msg.isSuccess == true) {
                                $.alert("数据删除成功", 1);
                                gMainDT.ajax.reload(null, false);
                            } else {
                                $.alert("数据删除失败", 2);
                            }
                        } else {
                            $.alert('系统错误，请联系管理员', 2);
                        }
                    },
                    error: function () {
                        $.alert('系统错误，请联系管理员', 2);
                    }
                });
            }
        }
    });
};

$.showSignalMachine = function (jsonData) {
    $("#projectNumber").val(jsonData.projectNumber);
    $("#signalMachineProvince").val(jsonData.signalMachineProvince);
    $("#signalMachineCity").val(jsonData.signalMachineCity);
    $("#signalMachineArea").val(jsonData.signalMachineArea);
    $("#coordinate").val(jsonData.signalMachineLongitude + "," + jsonData.signalMachineLatitude);
    $("#signalMachineStartRoad").val(jsonData.signalMachineStartRoad);
    $("#signalMachineEndRoad").val(jsonData.signalMachineEndRoad);
    $("#signalMachineBrand").val(jsonData.signalMachineBrand);
    $("#signalMachineInstallOrg").val(jsonData.signalMachineInstallOrg);
    $("#signalMachineInstallTime").val(jsonData.signalMachineInstallTime);
    $("#signalMachineRemark").val(jsonData.signalMachineRemark);
    $("#signalMachineOrgName").val(jsonData.signalMachineOrgName);
    $("#signalMachinePhotoImg").attr("src", jsonData.signalMachinePhoto);
    $("#signalMachinePeriod").val(jsonData.signalMachinePeriod);
    $("#signalMachineServiceLife").val(jsonData.signalMachineServiceLife);
    $("#signalMachineMainOrgName").val(jsonData.signalMachineMainOrgName);
    $("#signalMachineType").val(jsonData.signalMachineType);
    $("#signalMachineNumber").val(jsonData.signalMachineNumber);
    $("#signalMachineId").val(jsonData.signalMachineId);
    $("#signalMachineIsAlarm").val(jsonData.signalMachineIsAlarm);
    $("#signalMachineCreatorName").val(jsonData.signalMachineCreatorName);
    $("#signalMachineCreatorId").val(jsonData.signalMachineCreatorId);
    $("#signalMachineModifierId").val(gUserLoginResult.userId);
    $("#signalMachineOrgId").val(jsonData.signalMachineOrgId);
    $("#signalMachinePhoto").val(jsonData.signalMachinePhoto);

    $("#signalMachineModal4Audit").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
};

$.showAuditSignalMachine = function (signalMachineId) {
    $.auditSignalMachine(signalMachineId);
    $.clearModal($("#signalMachineForm"), $("#signalMachineModal4Audit"));
};

$("#myBtn").click(function () {
    $("#modalMapRoad").show();
    $("#startRoad").val($("#signalMachineStartRoad").val());
    $("#endRoad").val($("#signalMachineEndRoad").val());
    var url = "management/roadPoleMap.html";
    var coo = $("#coordinate").val(),
        coordinate,
        type = 1,
        x, y;
    if (coo != "") {
        coordinate = $("#coordinate").val().split(",");
        x = coordinate[0];
        y = coordinate[1];
    } else {
        x = $("#city").find("option:selected").text();
        y = $("#area").find("option:selected").text();
        type = 0;
    }
    url += "?x=" + x + "&y=" + y + "&z=" + type + "";//通过这种方式传参数
    $("#myMap").attr("src", url);
    $("#modal-map").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    })
});

$("#myBtn").click(function () {
    var coordinate = $("#coordinate").val().split(",");
    var x = coordinate[0];
    var y = coordinate[1];
    var url = "management/poleMap.html";
    url += "?x=" + x + "&y=" + y + "&z=1";//通过这种方式传参数
    $("#myMap").attr("src", url);
    $("#modal-map").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    })
});

